package com.resou.reader.reader.p;

import com.resou.reader.api.ApiImp;
import com.resou.reader.api.service.BookService;
import com.resou.reader.base.ResouBasePresenter;
import com.resou.reader.utils.ToastUtil;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class ReadSetPresenter extends ResouBasePresenter<ReadSetContract.View> implements ReadSetContract.Presenter {

    private final BookService mService;

    public ReadSetPresenter(ReadSetContract.View view) {
        super(view);
        mService = ApiImp.getInstance().getService(BookService.class);
    }

    @Override
    public void checkAutoBuy(String token, String bookId) {
        mView.showProgress();
        addCompositeDisposable(
                mService.checkAutoBuy(token, bookId)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                boolean data = result.getData();
                                mView.onSuccess(data, 0);
                            } else {
                                ToastUtil.makeLongToast(result.getMsg());
                            }
                        }, throwable -> {

                        }));
    }

    @Override
    public void setAutoBuy(String token, String bookId, String autoPerchase) {
        mView.showProgress();
        addCompositeDisposable(
                mService.setAutoBuy(token, bookId,autoPerchase)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(result -> {
                            if (result.getCode() == 0) {
                                mView.onSuccess(result, 1);
                            } else {
                                ToastUtil.makeLongToast(result.getMsg());
                            }
                        }, throwable -> {

                        }));
    }
}
